/**
 * Created by machan on 2017/5/10.
 */

import * as umfUtils from '../utils/UmfUtils';
import * as Constants from '../common/Constants';
import UmfLog from '../utils/UmfLog';

function cacheManager() {
    this.key = '';
    this.func = '';
    this.storage = {};

    this.init = (key, func)=> {
        let app = getApp();

        //缓存维度为代理商-业务员维度
        let funcKey = app.globalData.userData.agentId + app.globalData.userData.userId + key;
        this.key = funcKey;
        this.func = func;
        this.storage = {};
        return this;
    };

    this.cacheFunc = (params) => {
        if(!this.storage){
            this.storage = {};
        }
        Object.assign(this.storage, params);
        cacheFunc(this.key, this.func, params);
    };

    this.getFunc = () => {
        this.storage = getFunc(this.key, this.func);
        return this.storage;
    };

    this.clearFunc = ()=> {
        this.storage = {};
        clearFunc(this.key, this.func);
    }
}

/**
 * 缓存特定功能数据 异步缓存
 *
 * @param key
 * @param func
 * @param params
 */
function cacheFunc(key, func, params) {
    if (!key || !func || !params) {
        UmfLog.error("cacheStore error~");
        UmfLog.error("key= " + key +
            "func= " + func +
            "params= " + params);
        return;
    }

    wx.getStorage({
        key: key,
        success: function (res) {
            let data = res.data;
            if (data[func]) {
                data[func] = Object.assign(data[func], params);
            } else {
                data[func] = params;
            }

            wx.setStorage({
                key: key,
                data: data
            });
        },

        fail: function () {
            let data = {
                [func]: params
            };
            wx.setStorage({
                key: key,
                data: data
            });
        }
    });
}

/**
 *
 * 提取特定功能数据
 * @param key
 * @param func
 * @returns {*}
 */
function getFunc(key, func) {
    if (!key || !func) {
        UmfLog.error("getFunc error~");
        UmfLog.error("key= " + key +
            "func= " + func);
        return;
    }

    let data = umfUtils.getStorageSync(key);
    if (data) {
        return data[func];
    }
    return "";
}

/**
 * 清除特定功能数据
 * @param key
 * @param func
 */
function clearFunc(key, func) {
    if (!key || !func) {
        UmfLog.error("clearFunc error~");
        UmfLog.error("key= " + key +
            "func= " + func);
        return;
    }

    wx.getStorage({
        key: key,
        success: function (res) {
            let data = res.data;
            if (data && data[func]) {
                data[func] = null;
                wx.setStorage({
                    key: key,
                    data: data
                });
            }
        }
    });
}

/**
 * 保存用户维度相关缓存
 * @param userCache
 */
function saveUserCache(userCache) {
    let app = getApp();
    if (app) {
        let cacheKey = app.globalData.userData.agentId + "-" + app.globalData.userData.userId;
        let localCache = getFunc(cacheKey, Constants.Keys.USER_CACHE);
        localCache = localCache? localCache: {};
        let newCache = Object.assign(localCache, userCache);

        app.globalData.userCache = newCache;
        cacheFunc(cacheKey, Constants.Keys.USER_CACHE, newCache);
    }
}

/**
 * 保存代理商维度相关缓存
 * @param userCache
 */
function saveAgentCache(agentCache) {
    let app = getApp();
    if (app) {
        let cacheKey = app.globalData.userData.agentId;
        let localCache = getFunc(cacheKey, Constants.Keys.AGENT_CACHE);
        localCache = localCache? localCache: {};
        let newCache = Object.assign(localCache, agentCache);

        app.globalData.agentCache = newCache;
        cacheFunc(cacheKey, Constants.Keys.AGENT_CACHE, newCache);
    }
}

module.exports = {
    cacheManager: cacheManager,
    cacheFunc: cacheFunc,
    getFunc: getFunc,
    clearFunc: clearFunc,

    saveUserCache: saveUserCache,
    saveAgentCache: saveAgentCache
};